/* Includes ------------------------------------------------------------------*/
#include "wb32l003.h"
#include <stdio.h>
#include "MACRO.H"
#include "VALUE_SET.H"
#include "IO_SETUP.H"

uint8_t *TransmitBuf, *ReceiveBuf;
uint16_t TransmitCount, ReceiveCount;
uint8_t TransmitComplateFlag=0, ReceiveComplateFlag;
unsigned char RS_TIME=0;
unsigned char START_FIG=0;
unsigned char TIMER1_TIMES=0;
unsigned int	SOHW_BLINK=0;
unsigned char TX_TIME=0;
unsigned char addr_h=0;
unsigned int Autosave_sec=0;
unsigned int Autosave_min=0;
unsigned char Autosave_fig=0;
/*********************************************************************/
void TIM1_IRQHandler(void)                // 1mS
{
	if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET){
		TIMER1_TIMES++;
		if(TIMER1_TIMES>100)
			{TIMER1_TIMES=0;} //100mS
		
		Autosave_sec++;
		if(Autosave_sec>=1000){
			Autosave_sec=0;
			Autosave_min++;
			if(Autosave_min>=600){
				Autosave_min=0;Autosave_fig=1;
			}
		}
		
		SOHW_BLINK++; 
		if(SOHW_BLINK>2000){SOHW_BLINK=0;}
		
		RS_TIME++;
		if(RS_TIME==4){
			START_FIG=1;/*MODBUS_RX_Clean();RX_TIME=0;*/
		}         //485
		
		if(RS_TIME>100){RS_TIME=100;}         //485
		
		IP_INT++;
		if(IP_INT>10000){IP_INT=10000;}
		
		if(MODULE_FIG==0){
			TX_TIME++;
			if(TX_TIME>100){TX_TIME=100;}
		}
		
		TIM_ClearFlag(TIM1, TIM_FLAG_Update);
	}
}
/*********************************************************************/
void GPIOC_IRQHandler(void)
{if(GPIO_EXTI_GetFlagStatus(GPIOC, GPIO_Pin_4)){GPIO_EXTI_ClearFlag(GPIOC, GPIO_Pin_4);}   // T_DOUT
 if(GPIO_EXTI_GetFlagStatus(GPIOC, GPIO_Pin_6)){GPIO_EXTI_ClearFlag(GPIOC, GPIO_Pin_6);}}  // W_DOUT QQ
/*********************************************************************/
void GPIOD_IRQHandler(void)
{if(GPIO_EXTI_GetFlagStatus(GPIOD, GPIO_Pin_2)){GPIO_EXTI_ClearFlag(GPIOD, GPIO_Pin_2);}}  // DRDY
/*********************************************************************/
void UART1_IRQHandler(void)
{
	if(UART_GetFlagStatus(UART1, UART_FLAG_TI) != RESET){
		if (TransmitCount--)
		{
			UART_WriteData(UART1, *TransmitBuf++);
		}
		/* Transmit end */
		else
		{
			UART_ITConfig(UART1, UART_IT_TI, DISABLE);
			TransmitComplateFlag = 1;
		}
    
		UART_ClearFlag(UART1, UART_FLAG_TI);
	}	
	else if (UART_GetFlagStatus(UART1, UART_FLAG_RI) != RESET){
		RX_CMD = UART_ReadData(UART1);
		RXDATA[RX_TIME]=RX_CMD;
		RX_TIME++;
		TX_TIME=0;
		RS_TIME=0;
		
		UART_ClearFlag(UART1, UART_FLAG_RI);
//  RX_CMD = UART_ReadData(UART1);
//	if((RX_CMD==ADD_IP/0x100 || RX_CMD==0x00||RX_CMD==0x41	) && START_FIG==0)
//		{
//		 START_FIG=1;MODBUS_RX_Clean();
//		 RX_TIME=0;
//	 	}
//	TX_TIME=0;
//  RXDATA[RX_TIME]=RX_CMD;
//	RX_CMD=0;
//  RX_TIME++;
//	RS_TIME=0;
//  UART_ClearFlag(UART1, UART_FLAG_RI);
	}
}

